package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.FRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TCargotypeDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;

/**
 * 客运公司车型信息Service
 * @author wuhao
 * @version 2016-08-08
 */
@Service
@Transactional(readOnly = true)
public class TCargotypeService extends CrudService<TCargotypeDao, TCargotype> {
	
	@Autowired
	private TCargotypeDao cargoTypeDao;

	public TCargotype get(String cargotypeid) {
		return super.get(cargotypeid);
	}
	
	public List<TCargotype> findList(TCargotype tCargotype) {
		return super.findList(tCargotype);
	}
	
	//获取该城市所拥有的车型
	public List<TCargotype> findCityCargoList(String siteid) {
		return cargoTypeDao.findCityCargoList(siteid);
	}
	//获取车型
	public List<TCargotype> findTstylesList(String siteid,String tstyles) {
		return cargoTypeDao.findTstylesList(siteid,tstyles);
	}
	
	@SuppressWarnings("deprecation")
	public List<TCargotype> findalltypes() {
		//根据登陆用户的公司ID查询该公司拥有的所有车型
		User user = UserUtils.getUser();
		// 设置部门
		Office office = user.getOffice();	
		return dao.findTCargotypeByForsale(office.getId());
	}
	
	public Page<TCargotype> findPage(Page<TCargotype> page, TCargotype tCargotype) {
		if(tCargotype.getOffice() == null){
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tCargotype.setOffice(office);
		}
		return super.findPage(page, tCargotype);
	}
	
	@Transactional(readOnly = false)
	public void save(TCargotype tCargotype) {
		if(tCargotype.getCargotypeid() == null) {
			User user = UserUtils.getUser();
			tCargotype.setCreateBy(user);
			// 设置部门
			Office office = user.getOffice();
			tCargotype.setOffice(office);
			tCargotype.setCreateBy(user);
			tCargotype.setCreateDate(new Date());
			dao.insert(tCargotype);
		} else {
			dao.update(tCargotype);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TCargotype tCargotype) {
		super.delete(tCargotype);
	}
	
	/**
	 * 根据车型名称获取车型
	 * @param typename
	 * @return
	 */
	public TCargotype getCartypeByTypename(String typename) {
		return dao.findTCargotypeByTypename(typename);
	}

	public List<TCargotype> getCargotypeListForMobile() {
		return dao.getCargotypeListForMobile();
	}
}